package org.darkforest.admin.controller;

import org.apache.commons.lang.StringUtils;
import org.darkforest.annotation.ArchivesLog;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.ActiveAreaGoodsMapper;
import org.darkforest.model.ActiveAreaGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 专区
 *
 * @auth bingo
 * @create 2017-11-21-16:19
 */
@Controller
@RequestMapping(value = "operation")
public class ActiveAreaController {


    @Autowired
    private ActiveAreaGoodsMapper activeAreaGoodsMapper;

    /**
     * 专区列表 爱心助力 精神家园 创意家居 珍特优鲜
     * @param request
     * @return
     */
    @RequestMapping(value = "activeAreaList")
    public ModelAndView activeAreaList(HttpServletRequest request, String goodsName,
                                       @RequestParam(required = false, defaultValue = "axzl") String recommendType) {
        ModelAndView res = new ModelAndView("operation/area/active_area_list");
        ActiveAreaGoods activeAreaGoods= new ActiveAreaGoods(recommendType);
        //改页处理用
        String start = request.getParameter("start");
        if (start != null && !"".equals(start)) {
            activeAreaGoods.setStart(Integer.valueOf(start));
        }
        activeAreaGoods.setGoodsName(StringUtils.isBlank(goodsName)?null:goodsName);
        List<ActiveAreaGoods> activeAreaGoodsList = activeAreaGoodsMapper.selectByCondition(activeAreaGoods);
        activeAreaGoods.setResultList(activeAreaGoodsList);
        res.addObject("page",activeAreaGoods);
        return res;

    }


    /**
     * 删除专区商品
     * @param rId
     * @return
     */
    @ArchivesLog(operationDescription = "删除特色专区商品")
    @RequestMapping(value = "delActiveArea")
    public @ResponseBody boolean delActiveArea(int rId){
        int i = activeAreaGoodsMapper.deleteByPrimaryKey(rId);
        return i == 1?true:false;

    }

    /**
     * 修改展示状态
     * @param rId
     * @param display
     * @return
     */
    @ArchivesLog(operationDescription = "设置特色专区商品是否展示")
    @RequestMapping(value = "setActiveAreaDisplay")
    public @ResponseBody boolean setActiveAreaDisplay(int rId,int display){
        ActiveAreaGoods activeAreaGoods= new ActiveAreaGoods();
        activeAreaGoods.setrId(rId);
        activeAreaGoods.setDisplay(-(display)+"");
        activeAreaGoods.setUpdateTime(DateUtil.toTimeString(new Date()));
        int i = activeAreaGoodsMapper.updateByPrimaryKeySelective(activeAreaGoods);
        return i == 1?true:false;
    }


    /**
     * ajax设置排序号
     * @param rId
     * @param sort
     * @return
     */
    @RequestMapping(value = "setActiveAreaSort")
    @ArchivesLog(operationDescription = "设置特色专区商品排序")
    public @ResponseBody boolean setActiveAreaSort(int rId , int sort){
        ActiveAreaGoods activeAreaGoods= new ActiveAreaGoods();
        activeAreaGoods.setrId(rId);
        activeAreaGoods.setSort(sort);
        int i = activeAreaGoodsMapper.updateByPrimaryKeySelective(activeAreaGoods);
        return i == 1?true:false;

    }

    /**
     * ajax设置状态
     * @param rId
     * @param state
     * @return
     */
    @ArchivesLog(operationDescription = "设置特色专区商品状态")
    @RequestMapping(value = "setActiveAreaState")
    public @ResponseBody boolean setActiveAreaState(int rId , int state){
        ActiveAreaGoods activeAreaGoods= new ActiveAreaGoods();
        activeAreaGoods.setrId(rId);

        // state=1 ： 把正常->失效；state=0： 把失效->正常；
        if (state == 1) {
            activeAreaGoods.setState("0");
        } else {
            activeAreaGoods.setState("1");
        }
        activeAreaGoods.setUpdateTime(DateUtil.toTimeString(new Date()));
        int i = activeAreaGoodsMapper.updateByPrimaryKeySelective(activeAreaGoods);
        return i == 1?true:false;

    }


    /**
     * 修改商品所在专区
     * @param rId
     * @param recommendType
     * @return
     */
    @ArchivesLog(operationDescription = "修改特色商品所在专区")
    @RequestMapping(value = "changeGoodsRecommendArea")
    public @ResponseBody boolean changeGoodsRecommendArea(int rId , String recommendType){
        ActiveAreaGoods activeAreaGoods= new ActiveAreaGoods();
        activeAreaGoods.setrId(rId);
        activeAreaGoods.setRecommendType(recommendType);
        activeAreaGoods.setUpdateTime(DateUtil.toTimeString(new Date()));
        int i = activeAreaGoodsMapper.updateByPrimaryKeySelective(activeAreaGoods);
        return i == 1?true:false;

    }

}
